

#include <gtk/gtk.h>
#include "support.h"
#include "window.h"
#include "run.h"
#include "toolbar_second.h"
#include "config.h"

/*################ Globals ###################################*/
GtkWidget *start_address;
GtkWidget *run,*step;

/*########## prevent to eneter invalid address in the start address entry########*/
void on_address_changed(GtkEditable *was_changed,gpointer user_data)
{
	gchar *address,*tracker;
	gchar *to_insert;
	int i,j;
	to_insert=g_malloc(2);
	*(to_insert+1)='\0';
	address=gtk_editable_get_chars(was_changed,0,-1);
	tracker=address;
	i=g_utf8_strlen(address,-1);
	for(j=0;j<i;j++)
	{
		if(!g_ascii_isxdigit(*tracker))
		{
			gtk_editable_delete_text(was_changed,j,j+1);
		}
		else if(g_ascii_islower(*tracker))
		{
			*to_insert=g_ascii_toupper(*tracker);
			gtk_editable_delete_text(was_changed,j,j+1);
			gtk_editable_insert_text(was_changed,to_insert,1,&j);
		}
		tracker++;
	}
	g_free(to_insert);
}

/*################ get the second toolbar ####################*/
GtkWidget *gsim_get_toolbar_second( void )
{
	GtkWidget *hbox;
	hbox=gtk_hbox_new( FALSE , 0 );
	
	/*add separetor*/
	GtkWidget *sep1;
	sep1=gtk_vseparator_new( );
	gtk_box_pack_start(GTK_BOX(hbox),sep1,FALSE,FALSE,0);
	
	/*pack the label and entry*/
	GtkWidget *start_entry_label;
	start_entry_label=gtk_label_new(_("   Start Address "));
	gtk_box_pack_start(GTK_BOX(hbox),start_entry_label,FALSE,FALSE,0);
	
	start_address=gtk_entry_new_with_max_length(4);
	gtk_widget_set_size_request( start_address,60,-1);
	gtk_entry_set_text(GTK_ENTRY(start_address),gsim_values.start_address);
	gtk_box_pack_start(GTK_BOX(hbox),start_address,FALSE,FALSE,0);
	gtk_tooltips_set_tip(gsim_tooltips,start_address,_("Enter start address for execution"),NULL);
	g_signal_connect(start_address,"changed",G_CALLBACK(on_address_changed),NULL);
	
	/*add separetor*/
	GtkWidget *sep2;
	sep2=gtk_vseparator_new( );
	gtk_box_pack_start(GTK_BOX(hbox),sep2,FALSE,FALSE,10);
	
	/*make a size group*/
	GtkSizeGroup *hsizegroup;
	hsizegroup=gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
	
	/*pack run and step buttons*/
	run=gtk_button_new_from_stock(GSIM_STOCK_RUN);
	gtk_widget_set_size_request(run,70,-1);
	gtk_size_group_add_widget(hsizegroup,run);
	gtk_box_pack_start(GTK_BOX(hbox),run,FALSE,FALSE,5);
	gtk_tooltips_set_tip(gsim_tooltips,run,_("run the program"),NULL);
	g_signal_connect(run,"clicked",G_CALLBACK(gsim_run),NULL);
	
	step=gtk_button_new_from_stock(GSIM_STOCK_STEP);
	gtk_size_group_add_widget(hsizegroup,step);
	gtk_box_pack_start(GTK_BOX(hbox),step,FALSE,FALSE,5);
	gtk_tooltips_set_tip(gsim_tooltips,step,_("step through the program"),NULL);
	g_signal_connect(step,"clicked",G_CALLBACK(gsim_step),NULL);
	
	/*pack the box in the frame*/
	GtkWidget *frame;
	frame=gtk_frame_new( NULL );
	gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_NONE);
	gtk_container_add(GTK_CONTAINER(frame),hbox);
	
	return frame;
}
